﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agro.Models;
using SN.Data;
using System.Data;
using Agro.Library;
namespace Agro.Services
{
    public class MessageService : BaseService, IMessageService
    {

        public DataTable GetMessageList(SN.Core.QueryBase query, WhereClip wclip)
        {
            DataTable dt = DB.From<T_Message>()
                           .LeftJoin<T_Organization>("Receive", new Field("Receive].[Organization_ID") == T_Message._.Message_ReceiveOrgid)//此处加].[的原因是，如果不加，sqlserver自动会为字段变成[Receive.Organization_ID],这样就找不到该字段了，加上].[相当于将其分割开
                           .LeftJoin<T_Organization>("Send", new Field("Send].[Organization_ID") == T_Message._.Message_SendOrgid)
                           .LeftJoin<T_User>(T_User._.User_ID == T_Message._.Message_CreateUserid)
                           .Select(T_Message._.Message_SendOrgid, T_Message._.Message_Status, T_Message._.Message_Title, T_Message._.Message_Content
, T_Message._.Message_AttachmentName, T_Message._.Message_CreateTime, T_Message._.Message_ID, new Field("Receive].[Organization_Name").As("ReceiveOrgname"), new Field("Send].[Organization_Name").As("SendOrgname"), T_User._.User_Name).Where(wclip).ToTablePage(query);
            return dt;

        }

        public bool SaveMessage(T_Message Message, out string Msg, CurrentUser User)
        {
            Msg = string.Empty;
            using (DbTrans trans = DB.BeginTrans())
            {
                try
                {
                    Message.Message_SendOrgid = User.UserInfo.User_OrganizationID;
                    Message.Message_Title = Message.Message_Title;
                    Message.Message_Content = Message.Message_Content;
                    Message.Message_CreateUserid = User.UserInfo.User_ID.ToString();
                    Message.Message_CreateTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    Message.Message_Status = "0"; //阅读状态

                    Message.Detach();
                    trans.Save<T_Message>(Message);
                    //批量提交
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    Msg = ex.Message;
                    return false;
                }
            }
            return true;

        }

        public bool DeleteMessage(string ids, out string Msg)
        {
            Msg = string.Empty;
            int iCount = 0;
            try
            {
                iCount = DB.Delete<T_Message>(T_Message._.Message_ID.In(ids.Split(',')));
            }
            catch (Exception ex)
            {
                Msg = ex.Message;
            }
            return iCount > 0 ? true : false;

        }


        /// <summary>
        /// 获取当前用户未读的消息
        /// </summary>
        /// <param name="User"></param>
        /// <returns></returns>
        public string GetUserReadMessage(CurrentUser User)
        {
            string Msg = string.Empty;
            int rowcount = 1;
            List<T_Message> MessageList = (List<T_Message>)DB.From<T_Message>().Where(T_Message._.Message_ReceiveOrgid == User.UserInfo.User_OrganizationID && T_Message._.Message_Status != 1).ToList();
          //将最新发送的消息置于最上。
            MessageList = MessageList.OrderByDescending(m => m.Message_CreateTime).ToList(); ;
            foreach (var item in MessageList)
            {
                if (rowcount > 5) break;
                Msg += "<a href='javascript:SendMessage(" + item.Message_ID + ")'> " + (item.Message_Title.Length > 15? item.Message_Title.Substring(0, 15)+"..." : item.Message_Title) + "</a><br/>";
                rowcount++;
            }
            return Msg;
        }

        public void UpdateMessageStatus(string id, CurrentUser User)
        {
            DB.Update<T_Message>(T_Message._.Message_Status, "1", T_Message._.Message_ID == id && T_Message._.Message_Status != "1" && T_Message._.Message_ReceiveOrgid==User.UserInfo.User_OrganizationID);
        }

       
    }
}
